//
//  MZY_GuidePageController.m
//  FullHouse
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 yy. All rights reserved.
//

#import "MZY_GuidePageController.h"
#import "MZY_BrowseViewController.h"
#import "AllBarsHeader.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#define HORIZONTAL [UIScreen mainScreen].bounds.size.width / 414
#define VERTICAL [UIScreen mainScreen].bounds.size.height / 736

@interface MZY_GuidePageController ()<UITabBarControllerDelegate,UIScrollViewDelegate>

@property(nonatomic, retain)UIScrollView *scrollView;
@property(nonatomic, retain)UIPageControl *page;

@end

@implementation MZY_GuidePageController

- (void)dealloc
{
    [self.scrollView release];
    [self.page release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createGuide];
}

- (void)createGuide{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.scrollView.contentSize = CGSizeMake(WIDTH * 4, 0);
    self.scrollView.pagingEnabled = YES;
    [self.view addSubview:self.scrollView];
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self.scrollView release];
    
    UIImageView *firstPage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    firstPage.image = [UIImage imageNamed:@"page1.jpg"];
    [self.scrollView addSubview:firstPage];
    [firstPage release];
    
    UIImageView *secondPage = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT)];
    secondPage.image = [UIImage imageNamed:@"page2.jpg"];
    [self.scrollView addSubview:secondPage];
    [secondPage release];
    
    UIImageView *thirdPage = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * 2, 0, WIDTH, HEIGHT)];
    thirdPage.image = [UIImage imageNamed:@"page3.png"];
    [self.scrollView addSubview:thirdPage];
    [thirdPage release];
    
    UIImageView *fourthPage = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * 3, 0, WIDTH, HEIGHT)];
    fourthPage.image = [UIImage imageNamed:@"page4.jpg"];
    [self.scrollView addSubview:fourthPage];
    [fourthPage release];
    
    // 打开用户交互
    fourthPage.userInteractionEnabled = YES;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(WIDTH / 3 - 20, HEIGHT - 150, 150, 50);
    [button setTitle:@"立即体验" forState:UIControlStateNormal];
    button.layer.borderWidth = 1;
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    button.layer.cornerRadius = 10;
    [button addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside];
    [fourthPage addSubview:button];
    
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, HEIGHT - 20, WIDTH / 2, 20)];
    self.page.center = CGPointMake(WIDTH / 2, HEIGHT - 20);
    [self.view addSubview:self.page];
    self.page.numberOfPages = 4;
    self.page.currentPageIndicatorTintColor = [UIColor orangeColor];
    self.page.pageIndicatorTintColor = [UIColor whiteColor];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.page.currentPage = scrollView.contentOffset.x / WIDTH;
}

- (void)pressButton:(UIButton *)button{
    MZY_BrowseViewController *vc = [[MZY_BrowseViewController alloc]init];
    [self presentViewController:vc animated:NO completion:^{
        UITabBarController *tabVC = [[UITabBarController alloc]init];
        // 获取当前"window"
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        window.rootViewController = tabVC;
        
        MZY_BrowseViewController *browseVC = [[MZY_BrowseViewController alloc]init];
        UINavigationController *browseNAVC = [[UINavigationController alloc] initWithRootViewController:browseVC];
        browseNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"浏览" image:[UIImage imageNamed:@"browse.png"] tag:1001];
        
        MZY_InspirationViewController *insVC = [[MZY_InspirationViewController alloc]init];
        UINavigationController *insNAVC = [[UINavigationController alloc] initWithRootViewController:insVC];
        insNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"灵感" image:[UIImage imageNamed:@"inspiration.png"] tag:1002];
        
        MZY_CommunityViewController *comVC = [[MZY_CommunityViewController alloc]init];
        UINavigationController *comNAVC = [[UINavigationController alloc] initWithRootViewController:comVC];
        comNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"社区" image:[UIImage imageNamed:@"community.png"] tag:1003];
        
        MZY_ExpertViewController *expVC = [[MZY_ExpertViewController alloc]init];
        UINavigationController *expNAVC = [[UINavigationController alloc] initWithRootViewController:expVC];
        expNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"专家" image:[UIImage imageNamed:@"expert.png"] tag:1004];
        
        MZY_SelfViewController *selfVC = [[MZY_SelfViewController alloc]init];
        UINavigationController *selfNAVC = [[UINavigationController alloc] initWithRootViewController:selfVC];
        selfNAVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"self.png"] tag:1005];
        
        // 管理"vc"
        tabVC.viewControllers = @[browseNAVC,insNAVC,comNAVC,expNAVC,selfNAVC];
        // 取消半透明效果
        tabVC.tabBar.translucent = NO;
        // 设置"tabbar"颜色
        tabVC.tabBar.tintColor = [UIColor whiteColor];
        // 点击图标颜色
        tabVC.tabBar.tintColor = [UIColor orangeColor];
        // 设置代理人
        tabVC.delegate = self;
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
